<!-- 设备管理--基础设置-设备系统页面 -->
<template>
  <div class="equip-wrap-common">
    <px-form class="custom-form-ui equip-form" :model="queryParams" label-position="right" label-width="120px">
      <px-row :gutter="16">
        <px-col :span="5">
          <EquipFactorySelect />
        </px-col>
        <px-col :span="5">
          <EquipClassSelect :className="queryParams.className" @changeClassName="changeClassName" />
        </px-col>
        <px-col :span="5">
          <px-button type="primary" @click="queryTable">查询</px-button>
          <px-button @click="resetTable">重置</px-button>
        </px-col>
      </px-row>
    </px-form>
    <div class="equip-table-btn">
      <px-button v-has="'ALL_EQUIP_ARCHIVE_SETTING_SYSTEM_ADD'" type="primary" class="table-btn" @click="addEquip" style="margin-right: 8px"
        >新增设备系统</px-button
      >
      <px-button v-has="'ALL_EQUIP_ARCHIVE_SETTING_SYSTEM_DOWNLOAD'" style="margin-right: 8px" type="primary" plain @click="templateDownload"
        >下载模板</px-button
      >
      <BatchUpload
        url="/ifp/digitaltwins/api/v1/systemCategory/importData"
        permission="ALL_EQUIP_ARCHIVE_SETTING_SYSTEM_UPLOAD"
        @updateUploadPage="queryTable"
      />
    </div>
    <px-table
      :data="tableData"
      class="equip-table"
      size="small"
      height="calc(100vh - 295px)"
      border
      row-key="id"
      :tree-props="{ children: 'children' }"
    >
      <px-table-column
        v-for="(item, index) in tableColumns"
        :key="index"
        :label="item.label"
        :min-width="item.minWidth"
        :align="item.align"
        :show-overflow-tooltip="item.showOverflowTooltip"
        :fixed="item.fixed"
      >
        <template #default="scope">{{
          scope.row[item.prop] === 0 ? scope.row[item.prop] : scope.row[item.prop] ? scope.row[item.prop] : "-"
        }}</template>
      </px-table-column>
      <px-table-column label="操作" width="150" align="center">
        <template #default="scope">
          <px-tooltip content="编辑" placement="top">
            <px-button v-has="'ALL_EQUIP_ARCHIVE_SETTING_SYSTEM_EDIT'" link type="primary" icon="EditPen" @click="handleEdit(scope.row)" />
          </px-tooltip>
          <px-tooltip content="添加子级" placement="top">
            <px-button
              v-has="'ALL_EQUIP_ARCHIVE_SETTING_SYSTEM_ADD_CHILDREN'"
              link
              type="primary"
              icon="Plus"
              @click="handleAddChildren(scope.row)"
            />
          </px-tooltip>
          <px-tooltip content="删除" placement="top">
            <px-button v-has="'ALL_EQUIP_ARCHIVE_SETTING_SYSTEM_DELETE'" link type="danger" icon="Delete" @click="handleDelete(scope.row)" />
          </px-tooltip>
        </template>
      </px-table-column>
    </px-table>
    <!-- 新增/修改抽屉 -->
    <AddEquipDrawer
      :visible="equipVisible"
      :drawerType="drawerType"
      :equipRow="equipRow"
      :parentSystemName="parentSystemName"
      @queryTable="queryTable"
      @closeDrawer="closeDrawer"
    />
  </div>
</template>

<script setup lang="ts">
defineOptions({
  name: "ALL_EQUIP_ARCHIVE_SETTING_SYSTEM"
});
import { deleteEquipSystem, queryEquipSystem } from "@/api/equip/archive";
import EquipClassSelect from "@/views/all/equip/components/EquipClassSelect.vue";
import EquipFactorySelect from "@/views/all/equip/components/EquipFactorySelect.vue";
import { PxMessage, PxMessageBox } from "px-ui";
import { getCurrentInstance, onMounted, reactive, toRefs } from "vue";
import BatchUpload from "@/components/Upload/BatchUpload.vue";
import AddEquipDrawer from "./components/AddEquipDrawer.vue";

const { proxy }: any = getCurrentInstance();
const state = reactive<any>({
  queryParams: {
    className: ""
  },
  tableColumns: [
    { prop: "name", label: "系统名称", minWidth: "120", showOverflowTooltip: true },
    { prop: "factoryAreaDesc", label: "厂区", minWidth: "120", showOverflowTooltip: true },
    { prop: "classNameDesc", label: "科室", minWidth: "120", showOverflowTooltip: true },
    { prop: "createName", label: "创建人", minWidth: "120", showOverflowTooltip: true },
    { prop: "createTime", label: "创建时间", minWidth: "120", showOverflowTooltip: true }
  ],
  tableData: [],
  equipVisible: false,
  drawerType: "add",
  equipRow: {},
  parentSystemName: ""
});
const { queryParams, tableColumns, tableData, equipVisible, drawerType, equipRow, parentSystemName } = toRefs(state);

// 进入页面时触发
onMounted(() => {
  queryTable();
});
// 查询设备列表
const queryTable = async () => {
  const params = queryParams.value;
  const data = await queryEquipSystem(params);
  tableData.value = data || [];
};
// 重置筛选条件和设备列表
const resetTable = async () => {
  queryParams.value = {
    className: ""
  };
  queryTable();
};
// 下载模版
const templateDownload = async () => {
  proxy.download("get", "/digitaltwins/api/v1/systemCategory/templateDownLoad", {}, "设备系统模板.xlsx");
};
// 添加设备
const addEquip = () => {
  equipVisible.value = true;
  drawerType.value = "add";
};
// 编辑设备
const handleEdit = (row: any) => {
  equipRow.value = row;
  equipVisible.value = true;
  drawerType.value = "edit";
  tableData.value.forEach(item => {
    let recursionParent = function (it) {
      if ("children" in it) {
        it.children.forEach(item2 => {
          if (item2 == row) {
            parentSystemName.value = it.name;
          } else {
            recursionParent(item2);
          }
        });
      }
    };
    recursionParent(item);
  });
};
// 添加设备子级
const handleAddChildren = (row: any) => {
  equipRow.value = row;
  equipVisible.value = true;
  drawerType.value = "addChildren";
};
// 关闭抽屉
const closeDrawer = () => {
  equipVisible.value = false;
};
// 删除设备
const handleDelete = async (row: any) => {
  PxMessageBox.confirm(`要删除当前设备系统“${row.name}”吗？`, "提示", {
    confirmButtonText: "删除",
    confirmButtonClass: "px-button--danger",
    cancelButtonText: "取消",
    type: "warning",
    draggable: true
  })
    .then(async () => {
      await deleteEquipSystem({ id: row.id });
      PxMessage.success("删除成功");
      queryTable();
    })
    .catch(() => {});
};
// 课室组件调用父组件
const changeClassName = val => {
  queryParams.value.className = val;
};
</script>

<style lang="scss" scoped>
@import url("@/views/all/equip/scss/table-list.scss");

.equip-wrap-common {
  height: calc(100vh - 177px);
  .equip-form {
    padding-top: 12px;
  }
}
</style>
